import { createRouter, createWebHistory } from 'vue-router'
import Index from '@/views/Index/Index.vue'


import api from '@/api/'
import { UserInfoStore } from '@/stores/userInfoStore'
import { TagStore } from '@/stores/tagStore'


const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [
    { path: '/', name: 'root', component: Index, redirect: '/Index/Home', meta: { title: '根目录' } },
    { path: '/:catchAll(.*)*', name: 'err', component: () => import('@/views/err/404.vue'), hidden: true, meta: { title: '404页面' } },
    { path: '/login', name: 'login', component: () => import('@/views/Login.vue'), meta: { title: '登录页' } },


    { path: '/Index', 
      name: 'Index', 
      component: Index, 
      redirect: '/Index/Home', // 添加重定向配置
      meta: { title: '布局页', keepAlive: true },
      // 嵌套路由
      children: [
        { path: 'Home', name: 'Home', component: () => import('@/views/Index/Home.vue'), meta: { title: '首页', keepAlive: true }},
        { path: 'Discover', name: 'Discover', component: () => import('@/views/Index/Discover.vue'), meta: { title: '发现页' } },
        { path: 'Message', name: 'Message', component: () => import('@/views/Index/Message.vue'), meta: { title: '消息页' } },
        { path: 'Profile', name: 'Profile', component: () => import('@/views/Index/Profile.vue'), meta: { title: '个人主页', keepAlive: true } },
      ]
    },
    { path: '/OpusDetail', name: 'OpusDetail', component: () => import('@/views/OpusDetail.vue'), meta: { title: '帖子详情页' } },
    { path: '/Publish', name: 'Publish', component: () => import('@/views/Publish.vue'), meta: { title: '发布帖子页' } },
    { path: '/ProfileDetail', name: 'ProfileDetail', component: () => import('@/views/ProfileDetail.vue'), meta: { title: '个人资料详情页' } },
    { path: '/Search', name: 'Search', component: () => import('@/views/Search.vue'), meta: { title: '搜索页' } },
    { path: '/SearchResults', name: 'SearchResults', component: () => import('@/views/SearchResults.vue'), meta: { title: '搜索结果页' } },
    { path: '/FollowList', name: 'FollowList', component: () => import('@/views/FollowList.vue'), meta: { title: '关注/粉丝列表页' } },
    { path: '/UserProfile', name: 'UserProfile', component: () => import('@/views/UserProfile.vue'), meta: { title: '其他用户个人主页' } },
    { path: '/Chat', name: 'Chat', component: () => import('@/views/Chat.vue'), meta: { title: '聊天页面' } },
    { path: '/Blacklist', name: 'Blacklist', component: () => import('@/views/Blacklist.vue'), meta: { title: '黑名单管理页' } },
    { path: '/CommentReplies', name: 'CommentReplies', component: () => import('@/views/CommentReplies.vue'), meta: { title: '回复和@页面' } },
    { path: '/LikesReceived', name: 'LikesReceived', component: () => import('@/views/LikesReceived.vue'), meta: { title: '我的获赞页面' } },
  ],
})


// 注册全局前置守卫
router.beforeEach( async(to, from, next) => {
  
  const user_Store = UserInfoStore(); //这句代码不能放函数外面，要放在路由守卫内部，要等pinia初始化完毕
  const tag_Store = TagStore();

  try {
    // 如果无用户信息缓存
    if(user_Store.userInfo?.id == null){
      let jwtToken = localStorage.getItem(user_Store.userToken);
      if(jwtToken != null){
        let res = await api.User_GetUserInfoByJwt(jwtToken); // 进入初始页面时，会发送一次解析jwt的请求，若解析失败，全局的响应拦截器会提示登录已过期
        if(res.data.code == 1){
          user_Store.setUserInfo(res.data.data);
        }
      }
    }

    // 如果无标签信息缓存
    if(tag_Store.tagList.length == 0){
      let res = await api.Tag_GetNormalTag();
      if(res.data.code == 1){
        tag_Store.tagList = res.data.data;
      }
    }
  } catch (error) { // 即使获取数据失败，也继续路由，确保前端能正常渲染
    console.log('获取初始化数据失败，可能是后端服务不可用', error);
  }

  next();

})


export default router
